home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Pascal
/
Snippets
/
Caitlin 1.0
/
Caitlin.p
next >
Wrap
Text File
|
1994-03-15
|
6KB
|
169 lines
{Demo program posted on c.s.m.p by John B. Matthews in 1991.}
{Minor modifications, resource and project file added by Ingemar Ragnemalm 1994.}
{(Don't ask me what "Caitlin" is.)}
{Some comments on this demo:}
{• No real event processing, which means it's not a "real" program, just an animation until}
{the first mouse click. It does use GetNextEvent, though, so it does allow background tasks to}
{run. (This is no criticism: the demo has no ambition of being a "real" application.)}
{• The animation is not over a detailed background, only over a plain one. We don't really need}
{any offscreen at all to do this. We could simply DrawPicture to the window. The offscreen is}
{only used to hold the picture, which can be a speed advantage, but the PICT works fine too.}
{In other words, this is not what I would call an offscreen demo (like those using a full copy of}
{the screen image plus a second offscreen for a background).}
{• Color QD and GWorlds required – a rather unnecessary limitation for this demo. It's easy to}
{hack around that.}
{• It demonstrates the use of mask regions to make things move "behind" other things. This is,}
{IMHO, the best feature of the code.}
{From: jmatthews@desire.wright.edu}
{Newsgroups: comp.sys.mac.programmer}
{Subject: Re: Smooth Flicker Free Animation?}
{Message-ID: <1991Jul29.233551.4286@desire.wright.edu>}
{Date: 30 Jul 91 04:35:51 GMT}
{References: <1991Jul28.201432.23@iscsvax.uni.edu>}
{Organization: University Computing Services, Wright State University}
{Lines: 136}
{In article <1991Jul28.201432.23@iscsvax.uni.edu>, hewlitt8882@iscsvax.uni.edu writes:}
{> }
{> I'm sure this is a FAQ, but I haven't seen any postings of the}
{> answer. My question is, how does one have very smooth animation without}
{> flicker on the mac. I have look everywhere for some sample code, but to}
{> no avail. I'm trying to write a game on my Mac IIsi using Think C 4.0.5.}
{> If someone has some sample code for this procedure or some helpful hints,}
{> I'appreciate the information. Either post it here, or email it to me.}
{> }
{> Thanks in advanced.}
{> Robert Hewlitt Jr.}
{Here's my toy THINK Pascal 3.0 code for playing with offscreen animation. The}
{PICT (id=1000) has a two pixel border so it won't leave a "trail". The}
{CheckDirection proc calls THINK's NOTE proc just to make some noise when the}
{PICT "bounces" off a "wall". The maskRgn is purely decorative-just something}
{for the PICt to slide behind.}
{Minor changes for making it run under Think Pascal 4 /Ingemar}
program OffscreenTest;
uses
QDOffscreen;
var
wRect, pRect, dRect: Rect;
dh, dv: Integer;
picHdl: PicHandle;
picWorldPtr: GWorldPtr;
savePort: CGrafPtr;
saveGDH: GDHandle;
wPtr: WindowPtr;
maskRgn, edgeRgn: RgnHandle;
theEvent: EventRecord;
err: QDErr;
{Added by Ingemar:}
km: KeyMap;
hasEvent: Boolean;
procedure CalcRegions;
var
midH, midV: Integer;
tempRgn: RgnHandle;
begin
maskRgn := NewRgn;
edgeRgn := NewRgn;
tempRgn := NewRgn;
RectRgn(maskRgn, wRect);
InsetRgn(maskRgn, 35, 35);
with wRect do
begin
midH := (right - left) div 2;
SetRectRgn(tempRgn, midH - 4, top, midH + 4, bottom);
DiffRgn(maskRgn, tempRgn, maskRgn);
midV := (bottom - top) div 2;
SetRectRgn(tempRgn, left, midV - 4, right, midV + 4);
DiffRgn(maskRgn, tempRgn, maskRgn);
end;
RectRgn(tempRgn, wRect);
DiffRgn(tempRgn, maskRgn, edgeRgn);
FillRgn(edgeRgn, dkGray);
DisposeRgn(tempRgn)
end;
procedure CheckDirection;
begin
with dRect do
begin
if left < wRect.left then
begin
dh := -dh;
Note(300, 200, 1);
end;
if top < wRect.top then
begin
dv := -dv;
Note(500, 200, 1);
end;
if right > wRect.right then
begin
dh := -dh;
Note(350, 200, 1);
end;
if bottom > wRect.bottom then
begin
dv := -dv;
Note(250, 200, 1);
end
end
end;
begin
InitCursor;
wRect := screenBits.bounds;
InsetRect(wRect, 105, 105);
wPtr := NewCWindow(nil, wRect, 'Caitlin', false, noGrowDocProc, pointer(-1), true, 0);
wRect := wPtr^.portRect;
SetPort(wPtr);
ShowWindow(wPtr);
CalcRegions;
picHdl := PicHandle(Get1Resource('PICT', 1000));
pRect := picHdl^^.picFrame;
OffsetRect(pRect, -pRect.left, -pRect.top);
err := NewGWorld(picWorldPtr, 0, pRect, nil, nil, []);
if (err = noErr) and LockPixels(picWorldPtr^.portPixMap) then
begin
GetGWorld(savePort, saveGDH);
SetGWorld(picWorldPtr, nil);
EraseRect(pRect);
DrawPicture(picHdl, pRect);
SetGWorld(savePort, saveGDH);
dRect := pRect;
dh := 1;
dv := 1;
repeat
CopyBits(GrafPtr(picWorldPtr)^.portBits, GrafPtr(wPtr)^.portBits, pRect, dRect, srcCopy, maskRgn);
CheckDirection;
OffsetRect(dRect, dh, dv);
{Events: use GetNextEvent or GetOSEvent depending on whether or not alt is held down.}
{This was added by Ingemar to show the difference between allowing and disallowing background jobs.}
GetKeys(km);
if km[58] then
hasEvent := GetOSEvent(mDownMask + keyDownMask, theEvent)
else
hasEvent := GetNextEvent(mDownMask + keyDownMask, theEvent);
{We can replace GNE with WaitNextEvent(mDownMask + keyDownMask, theEvent, 1, nil);}
until hasEvent;
{until GetNextEvent(mDownMask + keyDownMask, theEvent); {old check.}
UnlockPixels(picWorldPtr^.portPixMap)
end;
DisposeRgn(maskRgn);
DisposeRgn(edgeRgn);
DisposeGWorld(picWorldPtr);
DisposeWindow(wPtr)
end.
{Of course, ther's no error chcking; it's just for fun.}
{Hope it helps. John}
{o----------------------------------------------------------------------------o}
{| John B. Matthews, jmatthews@desire.wright.edu, am103@cleveland.freenet.edu |}
{| "Now why would Mike Wallace be standing around on MY porch with a TV crew?"|}
{o----------------------------------------------------------------------------o}